<template>
	<xn-form-container
		:title="formData.id ? '查看' : '创建ncc单据'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="选择合同日期" name="motionFileId">
				<a-range-picker v-model:value="formData.dateRange" />
			</a-form-item>
			<a-form-item label="输入合同编号" name="motionFileId">
				<a-row :gutter="8">
					<a-col :span="17">
						<a-input v-model:value="formData.vbillcode" placeholder="请输入合同编号" show-search></a-input>
					</a-col>
					<a-col :span="7">
						<a-button class="xn-wd" @click="onSearchCtname"> 查询合同 </a-button>
					</a-col>
				</a-row>
			</a-form-item>

			<a-form-item label="请选择合同" name="incomeContract">
				<a-select
					v-model:value="formData.incomeContract"
					placeholder="请选择合同"
					show-search
					:options="ctNameOptions"
				/>
			</a-form-item>

			<a-form-item label="请选择物料" name="material">
				<a-select v-model:value="formData.material" placeholder="请选择物料" show-search :options="ctMaterialOptions" />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">创建</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="nccbackForm">
	import { cloneDeep } from 'lodash-es'
	import { message } from 'ant-design-vue'
	import bizAiNccApi from '@/api/biz/bizAiNccApi'
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const nccKey = ref([])
	const submitLoading = ref(false)
	const ctNameOptions = ref([])
	const ctMaterialOptions = ref([
		{
			label: '螺纹钢',
			value: '1001A110000000018OUY'
		},
		{ label: '镀锌管', value: '1001A110000000018RMB' },
		{ label: '热轧钢卷', value: '1001M910000000LSRJ0B' }
	])

	// 全局计时器
	let timeout
	// 上传文件数据
	const fileList = ref([])
	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		nccKey.value = record
		console.log(nccKey.value)
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		fileList.value = []
		open.value = false
	}
	// 默认要校验的
	const formRules = {}
	// 验证并提交数据
	const onSubmit = async () => {
		submitLoading.value = true
		const formDataParam = cloneDeep(formData.value)
		formDataParam.nccKey = nccKey.value
		let res = await bizAiNccApi.createGatherBill(formDataParam)
		console.log(res)
		submitLoading.value = false
	}
	// 查询收款合同
	const onSearchCtname = (val) => {
		if (timeout) {
			clearTimeout(timeout)
			timeout = null
		}
		let data = {}
		const formDataParam = cloneDeep(formData.value)
		if (formDataParam.dateRange != undefined) {
			timeout = setTimeout(async () => {
				const list = []
				data.startTime = formDataParam.dateRange[0]
				data.endTime = formDataParam.dateRange[1]
				data.vbillcode = formDataParam.vbillcode
				let res = await bizAiNccApi.getIncomeContract(data)
				console.log(res)
				res.forEach((r) => {
					list.push({
						value: r.fctPK + '-' + r.fctPKARbList,
						label: r.ctName,
						subList: r.fctPKARbList
					})
				})
				ctNameOptions.value = list
			}, 300)
		} else {
			message.info('请选择单据日期')
		}
	}

	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
